// pages/search/search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stickerLists: [],
    page: 1,
    pageTotal: 1,
    laodHide: true,
    statementHide: true,
    keywords: '',
    dataTips: '',
    currentPage: 1,
    pageTobottom:'到底了,亲，╮(￣▽￣")╭'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log("传回来关键词是：" + options.keywords);
    var keyWords = options.keywords;
    this.setData({
      keyWords: options.keywords
    })
    wx.request({
      url: app.globalData.searchUrl + keyWords, //接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        that.setData({
          stickerLists: res.data.data,
          page: res.data.page, //当前页
          pageTotal: res.data.pagetot, //页数
        });
        if (res.statusCode == 500) {
          console.log("获取失败")
          that.setData({
            dataTips: '好像没找到你想要的 ╮(￣▽￣")╭'
          })
        }
      },
      fail: function() {
        console.log("获取失败");
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showToast({
      title: '正在获取骚图',
      icon: 'loading'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function(options) {
    this.loadMoreSticker();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //显示眼睛
  eyesShow: function() {
    this.setData({
      showEyes: "show-eyes"
    })
  },
  //隐藏眼睛
  eyesHide: function() {
    this.setData({
        showEyes: ""
      }),
      console.log("隐藏眼球")
  },
  //监听表单
  searchValueInput: function(e) {
    var value = e.detail.value;
    console.log(value);
    if (value) {
      //输入不为空内容
      this.setData({
        keyWords: value.replace(/\s+/g, '')
      })
    } else {
      console.log("内容空的")
      this.setData({
        keyWords: null
      })
    }
  },
  //查看表情详细
  toStickerDetail: function(e) {
    var that = this;
    var stickerid = e.currentTarget.dataset.stickerid;
    console.log(stickerid);
    wx.navigateTo({
      url: '../sticker_detail/sticker_detail?stickerid=' + stickerid,
    })
  },
  //搜索请求
  searchThis: function() {
    var that = this;
    wx.showToast({
      title: '努力搜寻...',
      icon: 'loading'
    });
    wx.request({
      url: app.globalData.searchUrl + this.data.keyWords, //接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        that.setData({
          stickerLists: res.data.data,
          page: res.data.page, //当前页
          pageTotal: res.data.pagetot, //页数
        });
        if (res.statusCode == 500) {
          console.log("获取失败")
          that.setData({
            dataTips: '好像没找到你想要的 ╮(￣▽￣")╭'
          })
        }
      },
      fail: function() {
        console.log("获取失败");
      }
    });
  },
  //加载更多数据
  loadMoreSticker: function() {
    var that = this;
    var currentPage = this.data.page++;
    var oldStickerList = this.data.stickerLists;
    var pageTotal = this.data.pageTotal;
    this.setData({
      laodHide: false
    });
    //加载当前页面的下一页数据
    
    if (currentPage <= pageTotal){
      console.log("刷新页面第 " + (currentPage + 1) + " 个页面");
      wx.request({
        url: app.globalData.searchUrl + this.data.keyWords + "&p=" + (currentPage + 1), //接口地址 + 关键词 + 页数
        data: {},
        method: 'GET',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function(res) {
          //获取失败
          var newStickerList = res.data.data
          that.setData({
            stickerLists: oldStickerList.concat(newStickerList)
          })
          if (res.statusCode == 500) {
            console.log("获取失败")
            that.setData({
              dataTips: '好像没找到你想要的 ╮(￣▽￣")╭'
            })
          }
          that.setData({
            laodHide: true
          });
        },
        fail: function() {
          console.log("获取失败");
        }
      });
    }
    else{
      console.log("没数据了");
      this.setData({
        pageTobottom:"没数据了 O.O"
      })
    }
  }
})